package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.PopularEventsAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.dto.PopularEventDto;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 热门活动
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_popular_event)
public class PopularEventsActivity extends BasicListActivity {

	@ViewInject(R.id.events_list)
	private XListView mListView;

	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	private PopularEventsAdapter mAdapter;
	private List<PopularEventDto> datas = new ArrayList<PopularEventDto>();

	private final int REFRESH = 1;
	private final int LOADMORE = 2;
	protected boolean hasMore = false;
	private long cursorId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		init();
	}

	private void init() {
		mAdapter = new PopularEventsAdapter(getApplicationContext(), datas);
		mListView.setAdapter(mAdapter);
		mListView.setPullRefreshEnable(true); // 允许下拉刷新
		mListView.setPullLoadEnable(false); // 禁止加载更多
		mListView.setAutoLoadEnable(false); // 禁止自动加载
		mListView.setXListViewListener(this);// 加载监听

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (TextUtils.isEmpty(datas.get(position - 1).url)) {
					return;
				}
				Utils.jumpURL(PopularEventsActivity.this, datas.get(position - 1).url,
						datas.get(position - 1).title, datas.get(position - 1).imgUrl, false);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}

	@Override
	public void onPostSuccess(int postId, String result) {

		super.onPostSuccess(postId, result);
		Gson gson = new Gson();
		java.lang.reflect.Type type2 = new TypeToken<Page<PopularEventDto>>() {
		}.getType();
		Page<PopularEventDto> page = gson.fromJson(result, type2);
		hasMore = page.hasMore;

		if (page != null && page.records != null) {

			switch (postId) {
			case REFRESH:
				datas.clear();
				datas.addAll(page.records);
				break;
			case LOADMORE:
				datas.addAll(page.records);
				break;
			default:
				break;
			}

			if (datas.size() > 0) {
				cursorId = datas.get(datas.size() - 1).createdTime;
			} else {
				cursorId = 0;
			}

			mAdapter.notifyDataSetChanged();
		}

		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {
		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText("热门活动");
		home_title.setOnTouchListener(touch);
		onRequest(REFRESH, UrlConstant.POPULAR_EVENT, HttpMethod.GET, null);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onRefresh() {
		super.onRefresh();
		onRequest(REFRESH, UrlConstant.POPULAR_EVENT, HttpMethod.GET, null);
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();

		RequestParams params = new RequestParams();
		params.addBodyParameter("lastCursor", cursorId+"");

		onRequest(LOADMORE, UrlConstant.POPULAR_EVENT, HttpMethod.GET, params);
	}

	public XListView getXList() {
		return mListView;
	}

}
